package com.mapbased.wfw.components;

import java.io.*;
import java.util.*;

import org.dom4j.*;
import com.mapbased.wfw.*;

/**
 * <p>Title: </p>
 *
 * <p>Description:there's 2 way to using a border:using template
 * or using a border component (this component)</p>
 * when using template,the border content is extract to every page,so may
 * conside as waste memory
 * when using a border component,all pages share the same border page
 *
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class Border
    extends Component
{
    Page pageRef;
    String[] query[];

    /*   public static void main(String sfdsfdfsf[])
       {
           new Border().test();
       }

       private void test()
       {
           String page="/sssds/dsds.html?ss=&sdsdsds=sds";
           String[] ss=page.split("\\?");
           page=ss[0];
           if(ss.length == 2)
           {
               String p=ss[1];
               String pp[]=p.split("&");
               this.query=new String[pp.length][];
               for(int i=0;i < pp.length;i++)
               {

                   this.query[i]=pp[i].split("=");
               }

           }
           if(this.query != null)
           {
               for(int i=0;i < this.query.length;i++)
               {
                   String[] s=this.query[i];
                   if(s.length == 2)
                   {
                       System.out.println(s[0] + "=" + s[1]);
                   }
               }

           }

       }*/

    /**
     * a component can choose to parse an element or not.
     *
     * @param ele Element
     * @param loader PageLoader
     * @return Element or null:if wanna page loader parse the
     *   component,return the ele, else return null
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public Element parseXml(Element ele,
                            com.mapbased.wfw.PageLoader.LoadContext lc)
    {
        String page=ele.attributeValue("page");
        String[] ss=page.split("\\?");
        page=ss[0];
        if(ss.length == 2)
        {
            String p=ss[1];
            String pp[]=p.split("&");
            this.query=new String[pp.length][];
            for(int i=0;i < pp.length;i++)
            {

                this.query[i]=pp[i].split("=");
            }

        }

        pageRef=lc.loader.getPage(page,this.locale);
        if(pageRef==null)
        {
            throw new java.lang.RuntimeException("Cannot find border:".concat(page));
        }

        return ele;

    }

    /**
     * render
     *
     * @param context RenderContext
     * @throws IOException
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void render(RenderContext context)
        throws IOException
    {
        context.getRequestMap().put(
            this.pageRef,this);
        if(this.query != null)
        {
            for(int i=0;i < this.query.length;i++)
            {
                String[] s=this.query[i];
                if(s.length == 2)
                {
                    context.putParam(s[0],s[1]);
                }
            }

        }
        PageHandler old=context.getPageHandler();
        context.setPageHandler(pageRef.getPageHandler());

        pageRef.getPageHandler().defaultAction(context);
        context.setPageHandler(old);

        pageRef.defalutRenderBehavior(context);

    }

    /**
     * renderHeader
     *
     * @param context RenderContext
     * @param sb StringBuffer
     * @param m Map
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void renderHeader(RenderContext context,StringBuffer sb,Map m)
    {
        if(this.pageRef != null)
        {
            Component[] headables=this.pageRef.getHeadables();
            for(int i=0;i < headables.length;i++)
            {
                headables[i].renderHeader(context,sb,m);
            }
        }
    }

    public boolean isHeadable()
    {
        return this.pageRef != null && this.pageRef.getHeadables().length > 0;
    }
}
